package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sql.DBManagment;
import sql.record.*;
/**
 * Servlet implementation class checkUserExistance
 */
public class checkUserExistance extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public checkUserExistance() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User requestedUser = new User();
		requestedUser.userName = request.getParameter("username");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		try {
			List<User> searchResult = DBManagment.getRecord(requestedUser);
			if (searchResult.size() == 0){
				//username already exists
				out.print("<firefoxsucks>notExist</firefoxsucks>");
			}else{
				//username doesn't exist
				out.println("<firefoxsucks>exist</firefoxsucks>");
			}
			out.close();
		} catch (Exception e) {
			response.sendError(500, "Fatal Server Error!");
			e.printStackTrace();
		}
	}

}
